<?php

namespace app\commons\model;

use think\Model;
use think\model\concern\SoftDelete;

class Sale extends Model
{
    use SoftDelete;

    protected $deleteTime = 'delete_time';

    public $autoWriteTimestamp = true;

    public $createTime = 'createtime';

    /**
     * 获取转换json数据
     */
    public function getReplaceAttr($value){
        return json_decode($value,true);
    }

    /**
     * 获取转换json数据
     */
    public function getVideoAttr($value){
        $data = json_decode($value,true);
        if(isset($data['alivideo']['media']) && !empty($data['alivideo']['media'])){
            foreach ($data['alivideo']['media'] as $key=>$v){
                $data['alivideo']['media'][$key]['path'] = images_url($v['path']);
            }
        }
        return $data;
    }

    /**
     * 获取转换json数据
     */
    public function getPosterAttr($value){
        if($value){
            $value = json_decode($value,true);
            if(!empty($value)){
                foreach ($value as $k=>$v){
                    if(isset($value[$k]['src'])) $value[$k]['src'] = images_url($v['src']);
                }
            }
        }

        return $value;
    }

    /**
     * 获取转换json数据
     */
    public function getCardAttr($value){
        return json_decode($value,true);
    }

    /**
     * 格式化时间
     * @param $value
     * @return false|string
     */
    public function getStarttimeAttr($value){
        return date('Y-m-d',$value);
    }

    /**
     * 格式化时间
     * @param $value
     * @return false|string
     */
    public function getEndtimeAttr($value){
        return date('Y-m-d',$value);
    }

    /**
     * 关联活动视频
     * @return \think\model\relation\HasMany
     */
    public function activity_video()
    {
        return $this->hasMany(ActivityVideo::class,'sale_id','id');
    }
}